﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class TSSD0022
    { 
 
        #region ==AUFTYPE===========
        public static readonly DependencyProperty AUFTYPEProperty = DependencyProperty.Register("AUFTYPE",
            typeof(string), typeof(TSSD0022),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFTYPEChanged));
        public string AUFTYPE
        {
            get
            {
                return ((VSDDAT)MV).AUFTYPE;
            }
            set
            {
                SetValue(AUFTYPEProperty, value);
                ((VSDDAT)MV).AUFTYPE = value;
            }
        }
        private static void OnAUFTYPEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSSD0022;
            if (src != null)
            {
                src.AUFTYPE = (string)e.NewValue;
            }
        }
        #endregion
      
        public TSSD0022()
        {
            //AUFTYPE=//VKAUFTR
            //AUFKRS= 14 
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VSDDAT();
            DataContext = MV;
            SDSA = "0022"; 
            InitializeComponent(); 
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
                return;
            }
            KEY = dwMaster["KEY"].ToString();
            VALUE =Helpers.Helper.Right("000000"+ (Convert.ToDouble(dwMaster["VALUE"].ToString()) + 1).ToString(),6);
            dwMaster["VALUE"] = VALUE; 
            vwMaster.View.MoveCurrentTo(-1);
        } 
        public void Insert()
        {
            ((VSDDAT)MV).Insert(SDSA,KEY,AUFTYPE,8); 
        }
        public void Load()
        {
            dwData = ((VSDDAT)MV).SetData.Tables["SDDAT"].NewRow();
            dwData["SDSA"] = SDSA;
            dwData["SDKEY"] = " AND SDKEY='00" +AUFTYPE+ Helpers.Helper.Left( KEY.PadRight(2),2) + "          00'";
            SResult = ((VSDDAT)MV).QuerySDDAT(dwData, Convert.ToInt16(SDSA)); 
            if (SResult.Tables[0].Rows.Count == 0)
            {
                Insert();
                SResult = ((VSDDAT)MV).QuerySDDAT(dwData, Convert.ToInt16(SDSA));
            }  
            MergeData(SResult,false);
            vwMaster.View.MoveCurrentTo(-1);
        }
        public void Next()
        { 
            Load();
            vwMaster.View.MoveCurrentToNext();
        }
        public string GETNEXT()
        {
            string num = "";
            VALUE=((VSDDAT)MV).GETNEXT(SDSA,KEY, AUFTYPE);
            return VALUE; 
           
        }
       

        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsManage)
            {
                Load();
            }
            else
            {
                VICC_WFEVENTS.SVWF.SD02(RUNID, SDSA);
            }
            base.ManageCommand(sender, e);
        }
    }
}
